//
//  LiveSildingViewModel.m
//  project-demo-oc
//
//  Created by 赵隆杰 on 2021/6/15.
//

#import "LiveSildingViewModel.h"
#import "LiveSildingModel.h"

#define maxCacheNum 20 //缓存池最大缓存数
#define mimCacheNum 3  //上下滑最小缓存数

@implementation LiveSildingViewModel

- (NSMutableArray<LiveSildingModel *> *)playerList
{
    if (!_playerList) {
        _playerList = [[NSMutableArray<LiveSildingModel *> alloc] initWithCapacity:0];
    }
    return _playerList;
}
- (NSMutableArray<LiveSildingModel *> *)temUpPlayerList
{
    if (!_temUpPlayerList) {
        _temUpPlayerList = [[NSMutableArray<LiveSildingModel *> alloc] initWithCapacity:0];
    }
    return _temUpPlayerList;
}
- (NSMutableArray<LiveSildingModel *> *)temDownPlayerList
{
    if (!_temDownPlayerList) {
        _temDownPlayerList = [[NSMutableArray<LiveSildingModel *> alloc] initWithCapacity:0];
    }
    return _temDownPlayerList;
}

- (instancetype)init {
    self = [super init];
    if (self) {
//        self.dataSources = [[NSMutableArray alloc] init];

    }
    return self;
}

- (void)fetchData:(void(^)(NSMutableArray<LiveSildingModel *> *array))block
{
    //耗时0.5s模拟获取网络数据
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        block([self addData:self.playerList.count]);
    });
}

- (NSMutableArray<LiveSildingModel *> *)addData:(NSInteger)count
{
    NSMutableArray *array = [NSMutableArray array];
    for (NSInteger i = count; i < count + 10; i++) {
        LiveSildingModel *model = [[LiveSildingModel alloc] initWithId:@(i)];
        [array addObject:model];
    }
//    [self.dataSources addObjectsFromArray:array];
    return array;
}

-(void)first_fetchMoreVideoList:(void(^)(void))block withEnterId:(NSNumber *)id_
{
    //传入的数据添加到列表中  为避免冲突，这里写死id为10000，实际已传入为准
    LiveSildingModel *model = [[LiveSildingModel alloc] initWithId:id_];
    [self.playerList addObject:model];
    
    //首次请求10条数据
    [self fetchData:^(NSMutableArray<LiveSildingModel *> *array) {
        NSInteger middleCount = 0;
        if (array.count == 0) {
            //无数据，只显示传入数据，不能滑动
        } else if (array.count == 1) {
            //只有1条数据，只能下滑一个
        } else if (array.count % 2 == 0) {
            //偶数，上下平分
            middleCount = array.count / 2;
        } else if (array.count % 2 == 1) {
            //奇数，下滑多一个
            middleCount = (array.count - 1) / 2;
        }
        //前5条放下滑缓存池
        for (NSInteger i = 0; i < middleCount; i++) {
            [self.temDownPlayerList addObject:array[i]];
        }
        //后5条放上滑缓存池
        for (NSInteger i = middleCount; i < array.count; i++) {
            [self.temUpPlayerList addObject:array[i]];
        }
        
        //上滑缓存池取1条添加到列表第一个
        if (self.temUpPlayerList.count > 0) {
            LiveSildingModel *upModel = self.temUpPlayerList.firstObject;
            [self.playerList insertObject:upModel atIndex:0];
            [self.temUpPlayerList removeObjectAtIndex:0];
        }
        
        //下滑缓存池中取1条添加到列表后面
        if (self.temDownPlayerList.count > 0) {
            LiveSildingModel *downModel = self.temDownPlayerList.firstObject;
            [self.playerList addObject:downModel];
            [self.temDownPlayerList removeObjectAtIndex:0];
        }
        
        block();
    }];
    
}

/// 预请求 下滑
-(void)pre_fetchMoreVideoList:(void(^)(void))block
{
    //缓存池数量只有3个时去请求数据
    if (self.temDownPlayerList.count <= mimCacheNum) {
        [self fetchData:^(NSMutableArray<LiveSildingModel *> *array) {
            [self.temDownPlayerList addObjectsFromArray:array];
        }];
    }
    //从下滑缓存池中取出一条数据
    LiveSildingModel *downModel = self.temDownPlayerList.firstObject;
    if (downModel) {
        //数据添加到列表最后一个
        [self.playerList addObject:downModel];
        //取出列表第一个
        LiveSildingModel *temModel = self.playerList.firstObject;
        //将列表第一条数据移除
        [self.playerList removeObject:temModel];
        //从下滑缓存池中移除该数据
        [self.temDownPlayerList removeObject:downModel];
        //将列表移除数据添加到上滑缓存池中
        [self.temUpPlayerList insertObject:temModel atIndex:0];
        
        block();
    }
    
    //缓存池中超出一定数量数据移除掉
    if (self.temDownPlayerList.count + self.temUpPlayerList.count + 3 > maxCacheNum) {
        //需要移除个数
        NSInteger removeCount = self.temDownPlayerList.count + self.temUpPlayerList.count + 3 - maxCacheNum;
        if (removeCount > self.temUpPlayerList.count) {
            removeCount = self.temUpPlayerList.count;
        }
        [self.temUpPlayerList removeObjectsInRange:NSMakeRange(self.temUpPlayerList.count - removeCount, removeCount)];
    }
    
}

/// 预请求 上滑
- (void)pre_fetchMoreFrontVideoList:(void(^)(void))block
{
    //缓存池数量只有3个时去请求数据
    if (self.temUpPlayerList.count <= mimCacheNum) {
        [self fetchData:^(NSMutableArray<LiveSildingModel *> *array) {
            [self.temUpPlayerList addObjectsFromArray:array];
        }];
    }
    //从上滑缓存池中取出一条数据
    LiveSildingModel *upModel = self.temUpPlayerList.firstObject;
    if (upModel) {
        //数据添加到列表第一个
        [self.playerList insertObject:upModel atIndex:0];
        //取出列表最后一个
        LiveSildingModel *temModel = self.playerList.lastObject;
        //将列表最后一条数据移除
        [self.playerList removeObject:temModel];
        //从上滑缓存池中移除该数据
        [self.temUpPlayerList removeObject:upModel];
        //将列表移除数据添加到下滑缓存池中
        [self.temDownPlayerList insertObject:temModel atIndex:0];
        
        block();
    }
    
    //缓存池中超出一定数量数据移除掉
    if (self.temDownPlayerList.count + self.temUpPlayerList.count + 3 > maxCacheNum) {
        //需要移除个数
        NSInteger removeCount = self.temDownPlayerList.count + self.temUpPlayerList.count + 3 - maxCacheNum;
        if (removeCount > self.temDownPlayerList.count) {
            removeCount = self.temDownPlayerList.count;
        }
        [self.temDownPlayerList removeObjectsInRange:NSMakeRange(self.temDownPlayerList.count - removeCount, removeCount)];
    }
    
}

@end
